//
//  InviteFriendViewController.m
//  DaDaBao
//
//  Created by admin on 15/8/21.
//  Copyright (c) 2015年 User. All rights reserved.
//

#import "InviteFriendViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface InviteFriendViewController ()<UIAlertViewDelegate>
@property (nonatomic,strong)UIView * shareView;
@property (nonatomic,strong)UIView * shareBgView;
@end

@implementation InviteFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];

}
-(void)createUI
{
    TopView *topView =[[TopView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"邀请好友"];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, 20.0f, 64.0f, 44.0f)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
    [self.view addSubview:topView];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 100.0f, SCREEN_WIDTH, 20.0f)];
    titleLab.text = @"如此收益，还不喊上好友？";
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont boldSystemFontOfSize:20.0f];
    [self.view addSubview:titleLab];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-227/2, 100.0f+50.0f, 227.0f, 194.0f)];
    imageView.image = [UIImage imageNamed:@"invite"];
    [self.view addSubview:imageView];
    
    UIButton *inviteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    inviteBtn.frame = CGRectMake(10.0f, 400.0f, SCREEN_WIDTH-20.0f, 35.0f*HSCALE);
    [inviteBtn setBackgroundColor:[UIColor hexStringToColor:RED_COLOR_VALUE]];
    [inviteBtn setTitle:@"立即邀请" forState:UIControlStateNormal];
    inviteBtn.layer.cornerRadius = 5.0f;
    [inviteBtn addTarget:self action:@selector(buttonInviteTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:inviteBtn];

    

}
#pragma mark-邀请好友
-(void)buttonInviteTapped:(id)sender
{
//    弹出分享视图
    [self createTheShareView];
}
#pragma mark-------share

-(void)createTheShareView
{
    CGFloat btnWidth = (SCREEN_WIDTH-6)/4;
    CGFloat btnHeight = btnWidth;
    if (_shareView==nil) {
        
        _shareView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _shareView.userInteractionEnabled=TRUE;
        _shareView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
        
        UIWindow * window = [[UIApplication sharedApplication]keyWindow];
        [window addSubview:_shareView];
        
        UITapGestureRecognizer * gesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(letTheViewDown:)];
        gesture.numberOfTapsRequired=1;
        gesture.numberOfTouchesRequired=1;
        [_shareView addGestureRecognizer:gesture];
        
        
        _shareBgView=[[UIView alloc]init];
        _shareBgView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, SCREEN_WIDTH/2);
        _shareBgView.backgroundColor=[UIColor whiteColor];
        _shareBgView.userInteractionEnabled=TRUE;
        _shareBgView.backgroundColor = [UIColor hexStringToColor:@"#f8f8f8"];
        _shareBgView.clipsToBounds=TRUE;//为了让子视图也一起随着view做动画
        [_shareView addSubview:_shareBgView];
        
        //  创建shareBtn
        
        for (int i=0; i<8; i++) {
            NSArray *nameArr = @[@"微博",@"空间",@"微信",@"朋友圈",@"QQ",@"短信",@"邮件",@""];
            
            ShareButton *btn = [[ShareButton alloc] initWithFrame:CGRectMake(btnWidth*(i%4)+2*(i%4), btnHeight*(i/4)+2*(i/4), btnWidth, btnHeight) image:[UIImage imageNamed:[NSString stringWithFormat:@"sharing%d",i+1]] title:[nameArr objectAtIndex:i]];
            [btn setTitleLabelFont:[UIFont systemFontOfSize:12.0f]];
            btn.backgroundColor = [UIColor whiteColor];
            [btn addTarget:self action:@selector(buttonShareTheContentTapped:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = 1000+i;
            [_shareBgView addSubview:btn];
            
        }
        
        
    }
    
    [UIView animateWithDuration:0.4 animations:^{
        
        _shareBgView.hidden = FALSE;
        _shareView.hidden = FALSE;
        _shareBgView.frame = CGRectMake(0, SCREEN_HEIGHT - SCREEN_WIDTH/2, SCREEN_WIDTH, SCREEN_WIDTH/2);
        
    }];
    
}
#pragma mark-shareBtn方法
-(void)buttonShareTheContentTapped:(id)sender
{
    _shareBgView.userInteractionEnabled = FALSE;

    ShareButton *btn = (ShareButton *)sender;
    NSString *content = @"小额起投，马上注册有惊喜！大大宝带你轻松理财！@value(url)";
    
    switch (btn.tag) {
        case 1000:
        {
            [self createShareString:content withType:SSDKPlatformTypeSinaWeibo];
            
        }
            break;
        case 1001:
        {
            
            [self createShareString:content withType:SSDKPlatformSubTypeQZone];
        }
            break;
        case 1002:
        {
            [self createShareString:content withType:SSDKPlatformSubTypeWechatSession];
            
        }
            break;
        case 1003:
        {
            [self createShareString:content withType:SSDKPlatformSubTypeWechatTimeline];
            
        }
            break;
        case 1004:
        {
            [self createShareString:content withType:SSDKPlatformSubTypeQQFriend];
            
        }
            break;
        case 1005:
        {
            [self createShareString:content withType:SSDKPlatformTypeSMS];
            
            
        }
            break;
        case 1006:
        {
            [self sendMailInApp:content];
        }
            break;
        case 1007:
        {
            btn.enabled = FALSE;
        }
            break;
            
        default:
            break;
    }
}
-(void)createShareString:(NSString *)content withType:(SSDKPlatformType)type
{
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    

        [shareParams SSDKSetupShareParamsByText:content
                                         images:@[[UIImage imageNamed:@"Icon"]]
                                            url:[NSURL URLWithString:@"https://www.dadabao.net/client/jsp/portal/register.jsp"]
                                          title:@"大大宝"
                                           type:SSDKContentTypeAuto];
    
    
    //进行分享
    [ShareSDK share:type
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         
         switch (state) {
             case SSDKResponseStateSuccess:
             {
                 [self showAlert];
                 
                 break;
             }
             case SSDKResponseStateFail:
             {
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                     message:[NSString stringWithFormat:@"%@", [[error userInfo] objectForKey:@"error_message"]]
                                                                    delegate:self
                                                           cancelButtonTitle:@"确定"
                                                           otherButtonTitles:nil];
                 [alertView show];
                 break;
             }
             case SSDKResponseStateCancel:
             {
                [self snapViewBack];
                 
                 break;
             }
             default:
                 break;
         }
         
     }];
    
}
-(void)showAlert
{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                            message:nil
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
      
}
-(void)buttonCancelTheViewTapped:(id)sender
{
    [self snapViewBack];//将view滑下去
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self snapViewBack];
}
//view滑下去的方法
-(void)snapViewBack
{
    [UIView animateWithDuration:0.4 animations:^{
        
        _shareBgView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 224);
        
    } completion:^(BOOL finished) {
        _shareBgView.userInteractionEnabled = TRUE;

        _shareBgView.hidden = TRUE;
        _shareView.hidden = TRUE;
        
    }];
    
}

-(void)letTheViewDown:(UITapGestureRecognizer *)geture
{
    [self snapViewBack];
}

#pragma mark------------buttonTapped-------------

-(void)attitudeSnapShare:(UIButton *)btn
{
    [self snapViewBack];
}

#pragma mark-返回上一页
-(void)buttonBackTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:TRUE];
}

- (void)sendMailInApp:(NSString *)content
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        ALERT_VIEW(@"当前系统版本不支持应用内发送邮件功能，您可以使用mailto方法代替");
        [self snapViewBack];

        return;
    }
    if (![mailClass canSendMail]) {
        ALERT_VIEW(@"用户没有设置邮件账户");
        [self snapViewBack];
        return;
    }
    [self createShareString:content withType:SSDKPlatformTypeMail];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
